package com.atguigu.service.impl;

import com.atguigu.base.BaseDao;
import com.atguigu.base.BaseServiceImpl;
import com.atguigu.dao.CommunityDao;
import com.atguigu.dao.DictDao;
import com.atguigu.entity.Community;
import com.atguigu.service.CommunityService;
import com.atguigu.util.CastUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

@DubboService
@Transactional
public class CommunityServiceImpl  extends BaseServiceImpl<Community> implements CommunityService {
    @Autowired
    private CommunityDao communityDao;

    @Autowired
    private DictDao dictDao;


    @Override
    protected BaseDao<Community> getEntityDao() {
        return communityDao;
    }

    @Override
    public PageInfo<Community> findPage(Map<String, Object> filters) {
        // 页码数
        int pageNum = CastUtil.castInt(filters.get("pageNum"),1);
        // 每页记录数
        int pageSize = CastUtil.castInt(filters.get("pageSize"),10);
        // 开启分页
        PageHelper.startPage(pageNum,pageSize);
        Page<Community> page = getEntityDao().findPage(filters);
        //community  areaName  plateName  没有值
        //    areaId, plateId 有值
        List<Community> result = page.getResult();
        for (Community community : result) {
            community.setAreaName(dictDao.getNameById(community.getAreaId()));
            community.setPlateName(dictDao.getNameById(community.getPlateId()));
        }
        // 开启查询
        return new PageInfo<Community>(page,10);
    }

    @Override
    public Community getById(Serializable id) {
        Community community=communityDao.getById(id);
        // 为小区的areaName属性和plateName属性赋值
        community.setAreaName(dictDao.getNameById(community.getAreaId()));
        community.setPlateName(dictDao.getNameById(community.getPlateId()));
        return community;
    }

    @Override
    public List<Community> findAll() {
        List<Community> communityList =communityDao.findAll();
        for (Community community : communityList) {
            // 为小区的areaName属性和plateName属性赋值
            community.setAreaName(dictDao.getNameById(community.getAreaId()));
            community.setPlateName(dictDao.getNameById(community.getPlateId()));
        }
        return communityList;
    }
}
